package top.roadvast.rabbitmq.provider.simple;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.test.context.SpringBootTest;
import top.roadvast.rabbitmq.utils.ConnectionUtil;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

/**
 * 简单模式-生产者
 */
@Slf4j
@SpringBootTest
public class SimpleSend {

    private static final String QUEUE_NAME = "simple_queue";

    public static void main(String[] args) {
        // 获取到连接
        // 从连接中创建通道，使用通道才能完成消息相关的操作
        try (Connection connection = ConnectionUtil.getConnection();
             Channel channel = connection.createChannel();) {
            // 声明（创建）队列
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            final Scanner scanner = new Scanner(System.in);
            System.out.println("输入消息后按回车发送：");
            while (scanner.hasNext()) {
                final String input = scanner.nextLine();
                channel.basicPublish("", QUEUE_NAME, null,
                        input.getBytes());
                if (log.isDebugEnabled()) {
                    log.debug(" [x] Sent '" + input + "'");
                }
                System.out.println("输入消息后按回车发送：");
            }
        } catch (IOException | TimeoutException e) {
            e.printStackTrace();
        }
    }
}